package com.ylsk.check.service.impl;

import java.util.List;

import com.ylsk.check.domain.CheckItemRecord;
import com.ylsk.check.domain.CheckRecord;
import com.ylsk.check.domain.ReportInfo;
import com.ylsk.check.domain.dto.RectificationReportDTO;
import com.ylsk.check.domain.emum.ProblemStatusEnum;
import com.ylsk.check.domain.so.CheckProblemSO;
import com.ylsk.check.domain.vo.CheckProblemVO;
import com.ylsk.check.service.ICheckItemRecordService;
import com.ylsk.check.service.IRectificationReportService;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.DateUtils;
import com.ylsk.common.utils.SerialNoUtils;
import com.ylsk.system.helper.FileHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.CheckProblemMapper;
import com.ylsk.check.domain.CheckProblem;
import com.ylsk.check.service.ICheckProblemService;

/**
 * 施工检查问题Service业务层处理
 *
 * @author yangsy
 * @date 2023-12-21
 */
@Service
public class CheckProblemServiceImpl implements ICheckProblemService
{
    @Autowired
    private CheckProblemMapper checkProblemMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private SerialNoUtils serialNoUtils;

    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private IRectificationReportService rectificationReportService;

    @Autowired
    private ICheckItemRecordService checkItemRecordService;
    /**
     * 查询施工检查问题
     *
     * @param checkProblemId 施工检查问题主键
     * @return 施工检查问题
     */
    @Override
    public CheckProblemVO selectCheckProblemByCheckProblemId(Long checkProblemId)
    {
        //施工检查问题记录
        CheckProblemVO checkProblemVO = checkProblemMapper.selectCheckProblemByCheckProblemId(checkProblemId);
        //检查问题附件
        Long problemFileId = checkProblemVO.getProblemFileId();
        if(null != problemFileId){
            checkProblemVO.setProblemFile(fileHelper.getFileByBusinessId(problemFileId));
        }
        //整改附件
        Long rectificationFileId = checkProblemVO.getRectificationFileId();
        if(null != rectificationFileId){
            checkProblemVO.setRectificationFile(fileHelper.getFileByBusinessId(rectificationFileId));
        }
        //整改复查附件
        Long reviewFileId = checkProblemVO.getReviewFileId();
        if(null != reviewFileId){
            checkProblemVO.setReviewFile(fileHelper.getFileByBusinessId(reviewFileId));
        }

        //构建检查项记录
        CheckItemRecord checkItemRecord = new CheckItemRecord();
        checkItemRecord.setCheckRecordId(checkProblemVO.getCheckRecordId());
        List<CheckItemRecord> checkItemRecordList = checkItemRecordService.selectCheckItemRecordList(checkItemRecord);
        checkProblemVO.setCheckItemRecordList(checkItemRecordList);
        return checkProblemVO;
    }

    /**
     * 查询施工检查问题列表
     *
     * @param checkProblemSO 施工检查问题
     * @return 施工检查问题
     */
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<CheckProblemVO> selectCheckProblemList(CheckProblemSO checkProblemSO)
    {
        return checkProblemMapper.selectCheckProblemList(checkProblemSO);
    }

    /**
     * 新增施工检查问题
     *
     * @param checkProblem 施工检查问题
     * @return 结果
     */
    @Override
    public int insertCheckProblem(CheckProblem checkProblem)
    {
        checkProblem.setCheckProblemId(snowIdConfig.getSnowId());
        return checkProblemMapper.insert(checkProblem);
    }

    /**
     * 修改施工检查问题
     *
     * @param checkProblem 施工检查问题
     * @return 结果
     */
    @Override
    public int updateCheckProblem(CheckProblem checkProblem)
    {
        final Long checkProblemId = checkProblem.getCheckProblemId();
        if(null == checkProblemId){
            throw new ServiceException("检查问题id不能为空!");
        }

        checkProblem.setUpdateTime(DateUtils.getNowDate());
        return checkProblemMapper.updateCheckProblem(checkProblem);
    }

    /**
     * 批量删除施工检查问题
     *
     * @param checkProblemIds 需要删除的施工检查问题主键
     * @return 结果
     */
    @Override
    public int deleteCheckProblemByCheckProblemIds(Long[] checkProblemIds)
    {
        return checkProblemMapper.deleteCheckProblemByCheckProblemIds(checkProblemIds);
    }

    /**
     * 删除施工检查问题信息
     *
     * @param checkProblemId 施工检查问题主键
     * @return 结果
     */
    @Override
    public int deleteCheckProblemByCheckProblemId(Long checkProblemId)
    {
        return checkProblemMapper.deleteCheckProblemByCheckProblemId(checkProblemId);
    }

    @Override
    public int insertCheckProblem(CheckRecord checkRecord,ReportInfo reportInfo) {

        CheckProblem checkProblem = new CheckProblem();
        if(null == checkRecord.getCheckRecordId()){
            throw new ServiceException("施工检查记录id不能为空!");
        }
        checkProblem.setCheckProblemId(snowIdConfig.getSnowId());
        checkProblem.setCheckRecordId(checkRecord.getCheckRecordId());
        //  问题流水单号
        checkProblem.setCheckProblemNum(serialNoUtils.generateSerialNo(SerialNoUtils.SGWT_ORDER));
        //  问题类型
        Integer checkType = checkRecord.getCheckType();
        checkProblem.setProblenType(checkType);
        //  问题内容
        checkProblem.setProblemContent(checkRecord.getProblemDesc());
        //  负责人  项目报备创建人
        checkProblem.setHead(reportInfo.getCreateBy());
        //  问题状态
        checkProblem.setProblemStatus(ProblemStatusEnum.DRAFT.getCode());
        return checkProblemMapper.insert(checkProblem);
    }

    @Override
    public int saveRectificationReport(RectificationReportDTO rectificationReportDTO) {
        final Long checkProblemId = rectificationReportDTO.getCheckProblemId();
        if(null == checkProblemId){
            throw new ServiceException("checkProblemId 不能为空!");
        }

        //先上传整改问题
        rectificationReportService.insertRectificationReport(rectificationReportDTO);
        //更新状态 未已整改
        CheckProblem checkProblem = checkProblemMapper.selectById(checkProblemId);
        checkProblem.setProblemStatus(ProblemStatusEnum.SUBMIT.getCode());
        return checkProblemMapper.updateCheckProblem(checkProblem) ;
    }

    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<CheckProblemVO> selectUnReviewCheckProblemList(CheckProblemSO checkProblemSO) {
        return checkProblemMapper.selectUnReviewCheckProblemList(checkProblemSO);
    }
}
